﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.ENT;
using SIS.BLL;
using SIS.Utilities.TextString;

public partial class school_profile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadSchoolList();
            ddlSchoolList.DataBind();
            if (ddlSchoolList.Items.Count > 0)
            {
                ddlSchoolList.SelectedIndex = 0;
                ddlSchoolList_SelectedIndexChanged(sender, e);
            }
        }
    }

    //private void LoadSchoolList()
    //{
    //    ddlSchoolList.Items.Clear();
    //    SchoolProfileTableList _profileList = SchoolProfileTableManager.GetList();
    //    if (_profileList != null)
    //    {
    //        foreach (SchoolProfileTable _profile in _profileList)
    //        {
    //            ddlSchoolList.Items.Add(new ListItem(_profile.SchoolName, _profile.SchoolKey.ToString()));
    //        }
    //    }
    //}

    private void ShowSchoolProfile(SchoolProfileTable _schoolprofile)
    {
        RemoveHTMLTag _htmlTag = new RemoveHTMLTag();

        lblSchoolKey.Text = _schoolprofile.SchoolKey.ToString();
        lblSchoolKey.Text = Convert.ToString(_schoolprofile.SchoolKey);
        txtSchoolShortName.Text = _schoolprofile.SchoolShortName;
        txtSchoolName.Text = _schoolprofile.SchoolName;
        txtOtherSchoolName.Text = _schoolprofile.SchoolOtherName;

        txtSchoolBrief.Text = Server.HtmlDecode(_schoolprofile.SchoolBrief);

        txtSchoolOtherBrief.Text = Server.HtmlDecode(_schoolprofile.SchoolOtherBrief);

        if (_schoolprofile.RegisterYear > 0 && _schoolprofile.RegisterYear.ToString().Length == 4)
        {
            txtRegisterYear.Text = Convert.ToString(_schoolprofile.RegisterYear);
        }

        if (_schoolprofile.OpenYear > 0 && _schoolprofile.OpenYear.ToString().Length == 4)
        {
            txtOpenYear.Text = Convert.ToString(_schoolprofile.OpenYear);
        }

        
        txtOwnerName.Text = _schoolprofile.OwnerName;
        txtHeadMasterName.Text = _schoolprofile.HeadMasterName;
        txtRegistrarName.Text = _schoolprofile.RegistrarName;
        txtRegistratinoNo.Text = _schoolprofile.RegistrationNumber;
        txtWebsiteUrl.Text = _schoolprofile.WebsiteUrl;
        txtEmailAddress.Text = _schoolprofile.EmailAddress;
        txtFaxNumber.Text = _schoolprofile.FaxNumber;
        txtContactNo.Text = _schoolprofile.ContactNumber;
        txtRemark.Text = _schoolprofile.Remark;

        if (_schoolprofile.SchoolLogo != null && _schoolprofile.SchoolLogo.Length > 0)
        {
            divSchoolLogo.Visible = true;
            imgSchoolLogo.ImageUrl = "~/images/ImageHandler.ashx?key=" + _schoolprofile.SchoolKey.ToString() + "&type=schoollogo";
            hplSchoolLogo.NavigateUrl = imgSchoolLogo.ImageUrl;
            imgSchoolLogo.Width = 100;
            imgSchoolLogo.Height = 50;
        }
        else
        {
            divSchoolLogo.Visible = false;
            imgSchoolLogo.ImageUrl = "";
            hplSchoolLogo.NavigateUrl = "";
            imgSchoolLogo.Width = 0;
            imgSchoolLogo.Height = 0;
        }

        if (_schoolprofile.SchoolPhoto != null && _schoolprofile.SchoolPhoto.Length > 0)
        {
            divSchoolPhoto.Visible = true;
            imgSchoolPhoto.ImageUrl = "~/images/ImageHandler.ashx?key=" + _schoolprofile.SchoolKey.ToString() + "&type=schoolphoto";
            hplSchoolPhoto.NavigateUrl = imgSchoolPhoto.ImageUrl;
            imgSchoolPhoto.Width = 200;
            imgSchoolPhoto.Height = 100;
        }
        else
        {
            divSchoolPhoto.Visible = false;
            imgSchoolPhoto.ImageUrl = "";
            hplSchoolPhoto.NavigateUrl = "";
            imgSchoolPhoto.Width = 0;
            imgSchoolPhoto.Height = 0;
        }
        btnUpdateSchoolImage.Visible = true;
    }

    private void ClearSchoolProfile()
    {
        lblSchoolKey.Text = "-1";
        txtSchoolShortName.Text = "";
        txtSchoolName.Text = "";
        txtOtherSchoolName.Text = "";
        txtSchoolBrief.Text = "";
        txtSchoolOtherBrief.Text = "";
        txtRegisterYear.Text = "";
        txtOpenYear.Text = "";
        txtOwnerName.Text = "";
        txtHeadMasterName.Text = "";
        txtRegistrarName.Text = "";
        txtRegistratinoNo.Text = "";
        txtWebsiteUrl.Text = "";
        txtEmailAddress.Text = "";
        txtFaxNumber.Text = "";
        txtContactNo.Text = "";
        txtRemark.Text = "";
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            RemoveHTMLTag _htmlTag = new RemoveHTMLTag();
            SchoolProfileTable _schoolprofile = new SchoolProfileTable();
            _schoolprofile.SchoolKey = Convert.ToInt32(lblSchoolKey.Text);
            _schoolprofile.SchoolShortName = txtSchoolShortName.Text;
            _schoolprofile.SchoolName = txtSchoolName.Text;
            _schoolprofile.SchoolOtherName = txtOtherSchoolName.Text;

            if (txtSchoolBrief.Text.Length > 0)
            {
                txtSchoolBrief.Text = _htmlTag.GetPlainString(txtSchoolBrief.Text);
                txtSchoolBrief.Text = _htmlTag.ClearScriptTag(txtSchoolBrief.Text);
                txtSchoolBrief.Text = Server.HtmlEncode(txtSchoolBrief.Text);
                txtSchoolBrief.Text = _htmlTag.RemoveTailSpacing(txtSchoolBrief.Text);
                _schoolprofile.SchoolBrief = txtSchoolBrief.Text;
            }

            if (txtSchoolOtherBrief.Text.Length > 0)
            {
                txtSchoolOtherBrief.Text = _htmlTag.GetPlainString(txtSchoolOtherBrief.Text);
                txtSchoolOtherBrief.Text = _htmlTag.ClearScriptTag(txtSchoolOtherBrief.Text);
                txtSchoolOtherBrief.Text = _htmlTag.RemoveTailSpacing(txtSchoolOtherBrief.Text);
                txtSchoolOtherBrief.Text = Server.HtmlEncode(txtSchoolOtherBrief.Text);
                
                _schoolprofile.SchoolOtherBrief = txtSchoolOtherBrief.Text;
            }
            
            if (txtRegisterYear.Text.Length == 4)
            {
                _schoolprofile.RegisterYear = Convert.ToInt16(txtRegisterYear.Text);
            }

            if (txtOpenYear.Text.Length == 4)
            {
                _schoolprofile.OpenYear = Convert.ToInt16(txtOpenYear.Text);
            }

            _schoolprofile.OwnerName = txtOwnerName.Text;
            _schoolprofile.HeadMasterName = txtHeadMasterName.Text;
            _schoolprofile.RegistrarName = txtRegistrarName.Text;
            _schoolprofile.RegistrationNumber = txtRegistratinoNo.Text;
            _schoolprofile.WebsiteUrl = txtWebsiteUrl.Text;
            _schoolprofile.EmailAddress = txtEmailAddress.Text;
            _schoolprofile.FaxNumber = txtFaxNumber.Text;
            _schoolprofile.ContactNumber = txtContactNo.Text;
            _schoolprofile.Remark = txtRemark.Text;

            int _retKey = SchoolProfileTableManager.Save(_schoolprofile);

            if (_retKey > 0)
            {
                if (lblSchoolKey.Text == "-1")
                {
                    //LoadSchoolList();
                    ddlSchoolList.DataBind();
                }

                lblMessage.Text = "Saved successfully!";
                lblSchoolKey.Text = _retKey.ToString();
            }
            else
            {
                lblMessage.Text = "Saving failed!";
            }

        }
    }

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearSchoolProfile();
        btnShowSchoolProfile_Click(sender, e);
    }

    protected void btnAddNewSchool_Click(object sender, EventArgs e)
    {
        ClearSchoolProfile();
    }

    protected void btnShowSchoolProfile_Click(object sender, EventArgs e)
    {
        SchoolProfileTable _schoolprofile = SchoolProfileTableManager.GetItem(Convert.ToInt32(ddlSchoolList.Text));
        if (_schoolprofile != null)
        {
            ShowSchoolProfile(_schoolprofile);
        }
    }

    protected void btnUpdateSchoolImage_Click(object sender, EventArgs e)
    {
        Response.Redirect("photo-upload.aspx?id=" + ddlSchoolList.Text);
    }
}
